package com.qubuyer.business.home.view

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.qubuyer.R
import com.qubuyer.business.home.activity.SecondGoodListActivity
import com.qubuyer.business.home.adapter.HomeSpecialAdapter
import com.qubuyer.databinding.HomeItemSpecialBinding
import com.qubuyer.repository.entitys.HomeGoodEntity
import com.qubuyer.utils.NavigationUtil

class SpecItemView(lifecycleOwner: LifecycleOwner, context: Context,parent: ViewGroup, layoutId: Int) : HomeItemView<List<HomeGoodEntity>, HomeItemSpecialBinding>(lifecycleOwner, context,parent, layoutId) {

    fun bindChoiceList(data: List<HomeGoodEntity>?) {
        if (data.isNullOrEmpty()) {
            return
        }
        bindImpl(data, dataBinding.rvChoiceness, 1)
    }

    fun bindLimitList(data: List<HomeGoodEntity>?) {
        if (data.isNullOrEmpty()) {
            return
        }
        bindImpl(data, dataBinding.rvLimit, 2)
    }

    override fun bindView(data: List<HomeGoodEntity>) {
        dataBinding.host = this
        bindImpl(data, dataBinding.rvSpecial, 3)
    }

    fun bindPublishList(data: List<HomeGoodEntity>?) {
        if (data.isNullOrEmpty()) {
            return
        }
        bindImpl(data, dataBinding.rvFirstpublish, 4)
    }

    private fun bindImpl(data: List<HomeGoodEntity>, recyclerView: RecyclerView, type: Int) {
        var adapter = recyclerView.adapter as HomeSpecialAdapter?
        if (adapter == null) {
            adapter = HomeSpecialAdapter(ArrayList(data)) { NavigationUtil.overlay(mContext, SecondGoodListActivity::class.java, type) }
            recyclerView.layoutManager = GridLayoutManager(mContext, 2)
            recyclerView.adapter = adapter
        } else {
            adapter.setDataList(data)
        }
    }

    fun more(v: View) {
        NavigationUtil.overlay(mContext, SecondGoodListActivity::class.java, when (v.id) {
            R.id.ll_choiceness -> 1
            R.id.ll_limit -> 2
            R.id.ll_special -> 3
            R.id.ll_firstpublish -> 4
            else -> 1
        })
    }
}